// Engine.cpp : main project file.
#include "stdafx.h"
#include "engineManager.h"
#include "player.h"
#include "monster.h"
#include <string>
#include <sstream>
#include <time.h>
#include "item.h"

int main( int argc, char* args[] ) 
{ 
	player donovan;
	monster darkfall[3] =
	{
		monster(100, 100),
		monster(300, 100),
		monster(340, 220)
	};
	item potions[3] = 
	{
		item(0,0),
		item(0,0),
		item(0,0)
	};
	hitQueue logs[3];
	int frame = 0;
	int lastFPS = 0;
	float second = 0.0;
	Timer fps;
	Timer update; 
	Timer Delta;
	update.start(); 
	fps.start();
	Delta.start();
	bool quit = false;
	srand(time(NULL));
	//Initialize
	if(init() == false)
	{
		return 1;
	}
	//Load files
	if(load_files() == false)
		return 1;
	//While the user hasn't quit
	while(quit == false)
	{
		//While there's an event to handle
		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_KEYDOWN)
			{
				switch(event.key.keysym.sym)
				{
					case SDLK_w:
						donovan.yVel -= VELOCITY;
						break;
					case SDLK_s:
						donovan.yVel += VELOCITY;
						break;
					case SDLK_a:
						donovan.xVel -= VELOCITY;
						break;
					case SDLK_d:
						donovan.xVel += VELOCITY;
						break;
					case SDLK_SPACE:
					{
						for(int i = 0; i < 3; i++)
						{
							if(chkCol(potions[i].box, donovan.box))
							{
								potions[i].active = false;
								donovan.hpot+=1;
							}
						}
						break;
					}
					case SDLK_r:
					{
						if(donovan.hpot > 0) 
						{
							if(donovan.health < donovan.maxHealth)
							 {
								donovan.health += 25;
								if(donovan.health > donovan.maxHealth)
									donovan.health = donovan.maxHealth;
								donovan.hpot-=1;
							 }
						}
						break;
					}
				}
			}
			if(event.type == SDL_KEYUP)
			{
				switch(event.key.keysym.sym)
				{
					case SDLK_w:
						donovan.yVel += VELOCITY;
						break;
					case SDLK_s:
						donovan.yVel -= VELOCITY;
						break;
					case SDLK_a:
						donovan.xVel += VELOCITY;
						break;
					case SDLK_d:
						donovan.xVel -= VELOCITY;
						break;
				}
			}
			if(event.type == SDL_MOUSEBUTTONDOWN)
			{
				if(event.button.button == SDL_BUTTON_LEFT)
				{
					bool swung = false;
					for(int i = 0; i < 3; i++)
					{
						if(chkCol(donovan.range, darkfall[i].box) && darkfall[i].alive)
						{
							if(!donovan.swingDelay)
							{
								donovan.playerDamage(&darkfall[i], &logs[i], fontA, textColor);
								swung = true;
								
								if(!darkfall[i].alive)
								{
									potions[i].box.x = darkfall[i].box.x;
									potions[i].box.y = darkfall[i].box.y;
									potions[i].active = true;
								}
							}
						}
					}
					if(swung)
						donovan.swingDelay = true;
				}
			}
			if(event.type == SDL_QUIT)
			{
				//Quit the program
				quit = true;
			}
		}
		donovan.move(Delta.get_ticks());
		//if(logs[i].active)
			
		for(int i = 0; i < 3; i++)
		{
			
			if(!chkCol(darkfall[i].box, donovan.range))
				darkfall[i].follow(Delta.get_ticks(), donovan.x, donovan.y);
			if(chkCol(darkfall[i].box, donovan.range))
			{
				if(!darkfall[i].swingDelay)
				{
					donovan.health -= darkfall[i].attack(donovan.armor, donovan.dodgeChance);
					if(donovan.health <= 0)
						donovan.health = 0;
					darkfall[i].swingDelay = true;
				}
			}
		}
		if(second < 1.0)
		{
			second += Delta.get_ticks() / 1000.f;
			for(int i = 0; i < 3; i++)
				logs[i].move(Delta.get_ticks());
		}
		else if(second >= 1.0)
		{
			for(int i = 0; i < 3; i++)
			{
				if(!chkCol(darkfall[i].box, donovan.range))
					darkfall[i].frame++;
				
				if(!darkfall[i].alive)
					darkfall[i].respawn();
				darkfall[i].swingDelay = false;
			}
			donovan.swingDelay = false;
			second -= 1.f;
		}
		Delta.start();
		//Apply the surface to the screen
		apply_surface(0, 0, background, screen);
		apply_surface(donovan.x, donovan.y, playerIMG, screen);
		donovan.updateBars();
		donovan.lvlUp(fontA, textColor);
		SDL_FillRect( screen, &donovan.HBar, SDL_MapRGB( screen->format, 0, 0, 0 ) );
		SDL_FillRect( screen, &donovan.EBar, SDL_MapRGB( screen->format, 0, 155, 155 ) );
		apply_surface(donovan.levelQ.x, donovan.levelQ.y, donovan.levelQ.text, screen);
		for(int i = 0; i < 3; i++)
		{
			if(darkfall[i].alive)
			{
				apply_surface(darkfall[i].x, darkfall[i].y, monsterIMG, screen);
				darkfall[i].updateBar();
				SDL_FillRect( screen, &darkfall[i].HBar, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
			}
			if(potions[i].active)
			{
				apply_surface(potions[i].box.x, potions[i].box.y, potion, screen);
				apply_surface(potions[i].box.x, potions[i].box.y, gold, screen);
			}
			if(logs[i].active)
			{
				apply_surface(logs[i].x, logs[i].y, logs[i].text, screen);
			}
			
		}
		
		//Update screen
		if(SDL_Flip(screen) == -1)
		{
			return 1;
		}
		frame++;
		lastFPS = frame / ( fps.get_ticks() / 1000.f);
		if( update.get_ticks() > 1000 ) 
		{ 
			//The frame rate as a string 
			stringstream caption; 
			//Calculate the frames per second and create the string 
			caption << "Average Frames Per Second: " << frame / ( fps.get_ticks() / 1000.f ); 
			//Reset the caption 
			SDL_WM_SetCaption( caption.str().c_str(), NULL ); 
			//Restart the update timer 
			update.start(); 
		} 
		
		if(donovan.health == 0)
		{
			quit = true;
			SDL_WM_SetCaption( "GAME OVER! GAME OVER! YOU LOSE! GAME OVER! GAME OVER!", NULL );
			SDL_Delay(2000);
		}
		
	}
	clean_up();
	return 0; 
} 